package com.cheyunkeji.er.adapter.evaluate;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.evaluate.WaitToEvaluateActivity;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.evaluate.BeforeArchiveListItem;
import com.cheyunkeji.er.utils.TimeUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/3/23 17:50. */
public class WaitToEvaluateListAdapter extends MyBaseAdapter {

  OnItemActionListener listener;

  public WaitToEvaluateListAdapter(
      ArrayList<BeforeArchiveListItem> dataList, WaitToEvaluateActivity context) {
    super(dataList, context);
    listener = context;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_wait_to_evaluate_lv_item, null);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    final BeforeArchiveListItem bean = (BeforeArchiveListItem) dataList.get(position);

    //        未接收
    if (bean.getState().equals("2")) {

      holder.tvAction.setTextColor(context.getResources().getColor(R.color.btn_save_bkg_normal));
      holder.tvAction.setText("接收");

      //            已接收
    } else if (bean.getState().equals("3")) {
      holder.tvAction.setTextColor(context.getResources().getColor(R.color.blue_2));
      holder.tvAction.setText("去评估");
    }

    holder.tvCarModel.setText(String.format("车    型：%1$s", bean.getSpec_name()));
    holder.tvVinCode.setText(String.format("V   I   N : %1$s", bean.getVin()));
    holder.tvPerson.setText(String.format("推送人 : %1$s", bean.getPsuid_name()));
    holder.tvName.setText(bean.getOwner_realname());
    holder.tvContactNumber.setText(bean.getOwner_phone());
    holder.tvPalateNumber.setText(bean.getPlate());
    holder.tvDate.setText(
        String.format(
            context.getResources().getString(R.string.evaluate_date2),
            TimeUtils.millis2StringWithoutSeconds(
                Long.valueOf(String.valueOf(bean.getPushtime()) + "000"))));
    holder.tvAction.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {

            if (TextUtils.equals(bean.getState(), "2")) {

              listener.onReceive(position);
            } else if (TextUtils.equals(bean.getState(), "3")) {

              listener.goToEvaluate(position);
            }
          }
        });

    return convertView;
  }

  /** 按钮点击回调 */
  public interface OnItemActionListener {

    //        void onClickedListener(int position, String state);

    void goToEvaluate(int position);

    void onReceive(int position);
  }

  static class ViewHolder {
    @BindView(R.id.tv_contact_number)
    TextView tvContactNumber;

    @BindView(R.id.tv_name)
    TextView tvName;

    @BindView(R.id.tv_palate_number)
    TextView tvPalateNumber;

    @BindView(R.id.tv_car_model)
    TextView tvCarModel;

    @BindView(R.id.tv_vin_code)
    TextView tvVinCode;

    @BindView(R.id.tv_person)
    TextView tvPerson;

    @BindView(R.id.tv_action)
    TextView tvAction;

    @BindView(R.id.tv_date)
    TextView tvDate;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
